package com.inspector.app.ui.login

import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import com.alibaba.fastjson2.JSON
import com.example.commen.net.easyHttp.EasyHttpManager
import com.example.commen.net.easyHttp.model.HttpData
import com.example.commen.resource.CommentContect
import com.hjq.http.exception.ServerException
import com.hjq.http.listener.HttpCallback
import com.hjq.toast.Toaster
import com.inspector.app.di.network.api.MenuTreeApi
import com.inspector.app.di.network.api.SmsloginApi
import com.inspector.app.di.network.api.UserInfoApi
import com.inspector.app.di.network.api.getSmsLoginCodeApi
import com.inspector.app.di.network.api.loginApi
import com.inspector.app.util.CommenArgs
import com.inspector.app.util.SharedPreferencesUtils

class LoginModel : ViewModel() {


    /**
     * 获取验证码
     * @param mane 手机号
     * @param httpListener 请求反馈,true:获取成功
     */
    fun getAuthCode(
        lifecycleOwner: LifecycleOwner,
        name: String,
        httpListener: HttpCallback<SmsloginApi.loginResult>
    ) {
        EasyHttpManager.get().post(
            lifecycleOwner,
            getSmsLoginCodeApi(name),
            object : HttpCallback<SmsloginApi.loginResult>(httpListener) {
                override fun onSucceed(result: SmsloginApi.loginResult?) {
                    httpListener.onSucceed(result)
                }

                override fun onFail(e: Exception?) {
                    httpListener.onFail(e)
                }

            })
    }

    /**
     * 验证码登录
     */
    fun smsLogin(
        lifecycleOwner: LifecycleOwner,
        name: String,
        pwd: String,
        httpListener: HttpCallback<SmsloginApi.loginResult>
    ) {
        EasyHttpManager.get().post(
            lifecycleOwner,
            SmsloginApi(name, pwd),
            object : HttpCallback<SmsloginApi.loginResult>(httpListener) {
                override fun onSucceed(result: SmsloginApi.loginResult) {
                    loginSuccess(lifecycleOwner, result, httpListener)
                }

                override fun onFail(e: Exception?) {
                    httpListener.onFail(e)
                }
            })
    }

    /**
     * 登录成功
     */
    private fun loginSuccess(
        lifecycleOwner: LifecycleOwner,
        result: SmsloginApi.loginResult,
        httpListener: HttpCallback<SmsloginApi.loginResult>
    ) {
        SharedPreferencesUtils.put(
            CommentContect.SpConstant.LoginInfo,
            JSON.toJSONString(result.data)
        )
        CommenArgs.setIsLogin(true)
        CommenArgs.loginInfo = result.data
        getMenuTreeData(lifecycleOwner, object : HttpCallback<Boolean>(httpListener) {
            override fun onSucceed(it: Boolean) {
                if (it) {
                    httpListener.onSucceed(result)
                } else {
                    httpListener.onFail(ServerException("网络异常,请稍后再试"))
                }
            }
        })
    }

    /**
     * 密码登录
     */
    fun Login(
        lifecycleOwner: LifecycleOwner,
        name: String,
        pwd: String,
        httpListener: HttpCallback<SmsloginApi.loginResult>
    ) {
        EasyHttpManager.get()
            .post(
                lifecycleOwner,
                loginApi(name, pwd),
                object : HttpCallback<SmsloginApi.loginResult>(httpListener) {
                    override fun onSucceed(result: SmsloginApi.loginResult) {
                        loginSuccess(lifecycleOwner, result, httpListener)
                    }

                    override fun onFail(e: Exception?) {
                        httpListener.onFail(e)
                    }

                })
    }

    /**
     * 获取用户信息
     */
    fun getUserInfo(
        lifecycleOwner: LifecycleOwner,
        httpListener: HttpCallback<UserInfoApi.UserInfo>
    ) {
        EasyHttpManager.get()
            .get(
                lifecycleOwner,
                UserInfoApi(),
                object : HttpCallback<HttpData<UserInfoApi.UserInfo>>(httpListener) {
                    override fun onSucceed(result: HttpData<UserInfoApi.UserInfo>) {
                        CommenArgs.userInfo = result.data
                        SharedPreferencesUtils.put(
                            CommentContect.SpConstant.UserInfo,
                            JSON.toJSONString(CommenArgs.userInfo)
                        )
                        Log.i("getUserInfo", "onSucceed: " + JSON.toJSONString(CommenArgs.userInfo))
                        httpListener.onSucceed(result.data)
                    }

                    override fun onFail(e: Exception?) {
                        httpListener.onFail(e)
                    }

                })
    }

    /**
     * 退出登录
     */
    fun loginOut(): Boolean {
//        if (CommenArgs.getIsLogin()) {
//            CommenArgs.setIsLogin(false)
//            CommenArgs.loginInfo = LoginInfo()
//            CommenArgs.userInfo = null
//            SharedPreferencesUtils.remove(CommentContect.SpConstant.LoginInfo)
//            SharedPreferencesUtils.remove(CommentContect.SpConstant.UserInfo)
//            SharedPreferencesUtils.remove(CommentContect.SpConstant.MenuTree)
//            ActivityManager.getInstance().finishAllActivities(LoginActivity::class.java)
//            CommonUtils.EventPost(CommentContect.EventBusCode.LoginError)
//        }
        return true
    }

    /**
     * 获取菜单树
     * 包括底部导航
     * 包括我的机务item入口
     */
    fun getMenuTreeData(lifecycleOwner: LifecycleOwner, iCell: HttpCallback<Boolean>) {
        EasyHttpManager.get().get(
            lifecycleOwner,
            MenuTreeApi(),
            object : HttpCallback<HttpData<List<MenuTreeApi.MenuTrueInfoData>>>(iCell) {
                override fun onSucceed(result: HttpData<List<MenuTreeApi.MenuTrueInfoData>>) {
                    if (result.data != null) {
                        SharedPreferencesUtils.put(
                            CommentContect.SpConstant.MenuTree,
                            JSON.toJSONString(result.data)
                        )
                        CommenArgs.Trees.menuTree.addAll(result.data)
                        for (dat in result.data as List<MenuTreeApi.MenuTrueInfoData>) {
//                            if (dat.alias == MyJiWu) {//找到我的机务菜单
//                                CommenArgs.MyMenuTree = dat.children
//                                break
//                            }
                        }
                    }
                    iCell.onSucceed(true)
                }

                override fun onFail(e: Exception) {
                    Toaster.show(e.message)
                    iCell.onFail(e)
                }

            })
    }
}